package huang.teamwork15;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login3
 */
public class Login3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login3() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		User user=new User(username,pwd);
		UserService us=new UserService();
		boolean result=false;
		request.setAttribute("username", username);
		List<TransferRecords> tlist =new ArrayList<TransferRecords>();
		TransferRecords tr=new TransferRecords();
		TransferRecordsDao td=new TransferRecordsDao();
		try {
			tr=td.selectMessageByremitter(username);
//			tlist.add(tr);
//			request.setAttribute("tlist", tlist);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		tlist.add(tr);
		request.setAttribute("tlist", tlist);
		System.out.println(tlist.get(0).getPayee()+tlist.get(0).getTransferFund());
		try {
			result=us.login(user);
			if(result){
			request.getRequestDispatcher("MessageRecords.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("LoginFail.jsp").forward(request, response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
